package com.example.thirdstage_project.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.thirdstage_project.entity.Unit;
import com.example.thirdstage_project.entity.User;
import com.example.thirdstage_project.ov.LoginVO;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;


@Mapper
public interface UserDao extends BaseMapper<User>{
    /**
     * 登录
     * @param username,password
     * @return
     */
    @Select("select * from user where username=#{username} and password=#{password}")
    User login(@Param("username") String username, @Param("password") String password);

    /**
     * 多表联查 user和usergroup
     * 登录用户管理界面
     * @return
     */
    @Select("select a.username,b.gname from user a left join usergroup b on a.gid=b.gid order by a.uid")
    List<LoginVO> listByName();


    /**
     * 修改密码
     */
    @Update("update user set password=#{password}")
    int updatePassword(String password);

    /**
     * 新增
     */
    @Insert("insert into user value (gid=#{gid},username=#{username})")
    int save(User user);

    @Delete("SET FOREIGN_KEY_CHECKS=0" +
            " delete user,unit,expert " +
            "from ((user left join unit on user.uid=unit.uid) " +
            "left join expert on user.uid=expert.uid)" +
            "where user.uid=#{uid}")
    int deleteByUid(int uid);

    @Insert("insert into user value (username=#{username},password=#{password},gid=#{gid})")
    int updateOption(String username,String password,int gid);

    @Update("update unit set unitid=#{unitid},unitname=#{unitname},address=#{address},corporation=#{corporation}," +
            "investment=#{investment},type=#{type},tel=#{tel},introduction=#{introduction}")
    int updateEx(Unit unit);
}

